package com.darren.wordcount;

import java.util.ArrayList;
import java.util.List;


public class ReduceTask implements Runnable {

	private List<MapperTask> taskList = new ArrayList<MapperTask>();
	private boolean isRunning = true;
	
	
	public ReduceTask(List<MapperTask> taskList){
		if(null != taskList)this.taskList.addAll(taskList);
	}
	
	@Override
	public void run() {
System.out.println("reduceTask start...."+taskList);		
		while(isRunning){
System.out.println("reduce task....");
			//int unRunningTaskCount = 0;
			
			if(taskList.size() == 0)isRunning = false;
			
			List<MapperTask> toRemoveTasks = new ArrayList<MapperTask>();
			
			for(MapperTask task : taskList){
				if(task.isRunning())continue;
				
				StatisticResultsCache.add(task.getResult());
				
				toRemoveTasks.add(task);
				//taskList.remove(task);
			}
		System.out.println("toremovetasks:"+toRemoveTasks);
			if(toRemoveTasks.size() > 0)taskList.removeAll(toRemoveTasks);
				
			
		}
		
		
	}

}
